package com.kuqi.dao.impl;

import com.kuqi.dao.RoleDao;
import com.kuqi.domain.Role;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author:kuqi
 * @createTime:2021/10/16 11:00
 * @description:
 */
public class RoleDaoImpl implements RoleDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Role> getRoleList() {
        List<Role> roleList = jdbcTemplate.query("select * from sys_role",new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    @Override
    public void save(Role role) {
        jdbcTemplate.update("insert into sys_role values(?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    @Override
    public List<Role> getRoleListByUserId(Long id) {
        List<Role> roles = jdbcTemplate.query("select * from sys_user_role as ur join sys_role as r on ur.roleid=r.id where userid=?",new BeanPropertyRowMapper<Role>(Role.class),id);
        return roles;
    }
}
